package com.shiku.im.security.controller;

import com.shiku.common.model.PageResult;
import com.shiku.im.security.dto.WithdrawalDTO;
import com.shiku.im.security.entity.LineConfig;
import com.shiku.im.security.entity.ResourceInfo;
import com.shiku.im.security.entity.ResultDataSelect;
import com.shiku.im.security.entity.SecurityRole;
import com.shiku.im.security.service.Impl.LineConfigManagerImpl;
import com.shiku.im.security.service.ResourceInfoManager;
import com.shiku.im.security.service.SecurityRoleManager;
import com.shiku.im.user.entity.Role;
import com.shiku.im.user.service.impl.RoleManagerImpl;
import com.shiku.im.vo.JSONMessage;
import org.bson.types.ObjectId;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO (角色操作)
 * @Author xie yuan yang
 * @Date 2020/3/4
 **/
@ApiIgnore
@RestController
@RequestMapping("/console")
public class SecurityRoleController {

    @Autowired
    private SecurityRoleManager securityRoleManager;

    @Autowired
    private LineConfigManagerImpl lineConfigManager;
    
    @Autowired
    private ResourceInfoManager resourceInfoManager;

    @Autowired
    private RoleManagerImpl roleManager;

    @Autowired
    private RedissonClient redissonClient;

    /**
     * @Description //TODO  添加角色
     * @Date 11:25 2020/3/6
     **/
    @RequestMapping(value = "/addSecurityRole")
    public JSONMessage addSecurityRole(SecurityRole securityRole){
        SecurityRole securityRole1 = securityRoleManager.querySecurityRoleByRoleName(securityRole.getRoleName());
        if (securityRole1 != null){
            return JSONMessage.failure("角色已存在！");
        }
        List<Role> roles = roleManager.queryAllRole();
        int roleId = 0;
        for (Role role : roles) {
            if (role.getRole() > roleId){
                roleId = role.getRole();
            }
        }

        roleId++;
        securityRole.setRole((byte) roleId);
        securityRoleManager.sava(securityRole);
        return JSONMessage.success();
    }


    /**
     * @Description //TODO 删除角色
     * @Date 11:25 2020/3/6
     **/
    @RequestMapping(value = "/deleteSecurityRole")
    public JSONMessage deleteSecurityRole(String id){
        securityRoleManager.deleteRole(new ObjectId(id));
        return  JSONMessage.success();
    }

    /**
     * @Description //TODO  修改角色
     * @Date 11:25 2020/3/6
     **/
    @RequestMapping(value = "/updateSecurityRole")
    public JSONMessage updateSecurityRole(SecurityRole securityRole){
        securityRoleManager.updateRole(securityRole);
        return JSONMessage.success();
    }

    /**
     * @Description //TODO  修改角色资源权限
     * @Date 11:25 2020/3/6
     **/
    @RequestMapping(value = "updateRoleResource")
    public JSONMessage updateRoleResource(String roleId,String auth){
        List<String> list = new ArrayList<>();
        String[] split = auth.split(",");
        for (String s : split) {
            list.add(s);
        }
        securityRoleManager.updateRoleResource(new ObjectId(roleId),list);
        return JSONMessage.success();
    }

    /**
     * @Description //TODO 查询全部角色
     * @Date 11:25 2020/3/6
     **/
    @RequestMapping(value = "/querySecurityRole")
    public JSONMessage querySecurityRole(String page, String limit){
        PageResult<SecurityRole> data = securityRoleManager.querySecurityRole(Integer.parseInt(page), Integer.valueOf(limit));
        /*for (SecurityRole d : data.getData()) {
            for (int i = 0; i < d.getRoleResourceList().size(); i++) {
                if (!d.getRoleResourceList().get(i).equals("")){
                    ResourceInfo resourceInfo = resourceInfoManager.queryResourceInfoById(new ObjectId(d.getRoleResourceList().get(i)));
                    d.getRoleResourceList().set(i,resourceInfo.getResourceName());
                }
            }
        }*/
        return JSONMessage.success(data);
    }

    @RequestMapping(value = "/withdrawalConfig")
    public JSONMessage withdrawalConfig(String page, String limit){
        RList<WithdrawalDTO> withdrawalConfig = redissonClient.getList("withdrawalConfig");


        List<WithdrawalDTO> list = withdrawalConfig.range(0, withdrawalConfig.size() - 1);
        //分页 list 模拟分页
        list = list.stream()
                .skip((Integer.parseInt(page) - 1) * Integer.parseInt(limit))
                .limit(Integer.parseInt(limit))
                .collect(Collectors.toList());;
        PageResult<WithdrawalDTO> data = new PageResult<>();
        data.setData(list);
        data.setTotal(withdrawalConfig.size());
        return JSONMessage.success(data);
    }

    @RequestMapping(value = "/withdrawalUpdateConfig")
    public JSONMessage withdrawalUpdateConfig(WithdrawalDTO param){
        RList<WithdrawalDTO> withdrawalConfig = redissonClient.getList("withdrawalConfig");
        List<WithdrawalDTO> list = withdrawalConfig.range(0, withdrawalConfig.size() - 1);
        for (WithdrawalDTO d : list) {
            if(d.getId().equals(param.getId())){
                withdrawalConfig.remove(d);
                withdrawalConfig.add(param);
                break;
            }
        }

        return JSONMessage.success();
    }

    @RequestMapping(value = "/withdrawalDelConfig")
    public JSONMessage withdrawalDelConfig(WithdrawalDTO param){
        RList<WithdrawalDTO> withdrawalConfig = redissonClient.getList("withdrawalConfig");
        List<WithdrawalDTO> list = withdrawalConfig.range(0, withdrawalConfig.size() - 1);
        for (WithdrawalDTO d : list) {
            if(d.getId().equals(param.getId())){
                withdrawalConfig.remove(d);
                break;
            }
        }

        return JSONMessage.success();
    }

    @RequestMapping(value = "/withdrawalAddConfig")
    public JSONMessage withdrawalAddConfig(WithdrawalDTO param){
        RList<WithdrawalDTO> withdrawalConfig = redissonClient.getList("withdrawalConfig");
        if(withdrawalConfig.size() > 0){
            return JSONMessage.failure("只能添加一条配置");
        }
        List<WithdrawalDTO> list = withdrawalConfig.range(0, withdrawalConfig.size() - 1);
        param.setId(list.stream().map(WithdrawalDTO::getId)
                .max(Integer::compareTo).orElse(0) + 1);
        withdrawalConfig.add(param);

        return JSONMessage.success();
    }



    @RequestMapping(value = "/listLineConfig")
    public JSONMessage listLineConfig(String page, String limit){
        PageResult<LineConfig> data = lineConfigManager.queryLineConfigList(Integer.parseInt(page), Integer.valueOf(limit));
        return JSONMessage.success(data);
    }

    @RequestMapping(value = "/deleteLineConfig")
    public JSONMessage deleteLineConfig(String id){
        lineConfigManager.deleteLineConfig(new ObjectId(id));
        return  JSONMessage.success();
    }

    @RequestMapping(value = "/saveLineConfig")
    public JSONMessage saveLineConfig(LineConfig lineConfig){
        return  JSONMessage.success(lineConfigManager.sava(lineConfig));
    }

    /**
     * @Description //TODO 根据id查询角色
     * @Date 18:32 2020/3/6
     **/
    @RequestMapping(value = "/querySecirutyRoleById")
    public JSONMessage querySecirutyRoleById(String id){
        SecurityRole securityRole = securityRoleManager.querySecurityRoleById(new ObjectId(id));
        return JSONMessage.success(securityRole);
    }

    /**
     * @Description //TODO 查询全部角色
     * @Date 11:25 2020/3/6
     **/
    @RequestMapping(value = "/querySecurityRole1")
    public JSONMessage querySecurityRole1(){
        List<SecurityRole> securityRoles = securityRoleManager.querySecurityRol1e();
        /*for (SecurityRole securityRole : securityRoles) {
            securityRole.setRoleId1(String.valueOf(securityRole.getRoleId()));
        }
        System.out.println(JSONObject.toJSON(securityRoles));*/
        List<ResultDataSelect> data = new ArrayList<>();
        for (SecurityRole securityRole : securityRoles) {
            ResultDataSelect resultDataSelect = new ResultDataSelect();
            resultDataSelect.setName(securityRole.getRoleName());
            resultDataSelect.setValue(String.valueOf(securityRole.getRoleId()));
            data.add(resultDataSelect);
        }

        return JSONMessage.success(data);
    }


    @RequestMapping(value = "/queryRoleResourceInfo")
    public JSONMessage queryRoleResourceInfo(String roleId){
        SecurityRole securityRole = securityRoleManager.querySecurityRoleById(new ObjectId(roleId));
        List<ResourceInfo> data = new ArrayList<>();
        List<String> roleResourceList = securityRole.getRoleResourceList();
        for (String s : roleResourceList) {
            if (!s.equals("")){
                ResourceInfo resourceInfo = resourceInfoManager.queryResourceInfoById(new ObjectId(s));
                data.add(resourceInfo);
            }
        }
        return JSONMessage.success(data);
    }

    @RequestMapping(value = "/queryRoleResourceId")
    public JSONMessage queryRoleResourceId(String roleId){
        SecurityRole securityRole = securityRoleManager.querySecurityRoleById(new ObjectId(roleId));
        List<String> data = new ArrayList<>();
        List<String> roleResourceList = securityRole.getRoleResourceList();
        if (roleResourceList != null){
            for (String s : roleResourceList) {
                if (!s.equals("")){
                    //ResourceInfo resourceInfo = resourceInfoManager.queryResourceInfoById(new ObjectId(s));
                    data.add(s);
                }
            }
        }
        return JSONMessage.success(data);
    }

}
